﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFine.Code;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.Project;
using ZFine.Repository.Project;

namespace ZFine.Application.Project
{
    public class ChargingStandardApp
    {
        private IChargingStandardRepository service = new ChargingStandardRepository();
        
        public Log FileLog
        {
            get { return LogFactory.GetLogger(this.GetType().ToString()); }
        }
        /// <summary>
        /// 查全表
        /// </summary>
        /// <returns></returns>
        public List<ChargingStandardEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ChargingStandardEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyValue"></param>
        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                local.State = false;
                return service.Update(local);
            }
            else
            {
                return -1;
            }
        }

        #region 模糊查询
        public List<ChargingStandardEntity> GetListlist(Pagination pagination, string keyword)//参数1-分页类  2-形参
        {
            var expression = ExtLinq.True<ChargingStandardEntity>();//ExtLinq这个是sql查询
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.Name.Contains(keyword));
            }
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);//FindList
        }

        #endregion

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="SlideShow"></param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public int SubmitForm(ChargingStandardEntity Charge, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                //if (service.IQueryable(f => f.Name == Charge.Name) != null)
                //{
                //    return -2;
                //}
                if (!string.IsNullOrEmpty(keyValue))
                {
                    var historyent = service.FindEntityAsNoTracking(f => f.F_Id == keyValue);
                    if (historyent == null)
                    {
                        return -3;
                    }
                    Charge.Modify(keyValue);
                    string intostring = string.Format("修改了价格，标题为：{0}，价格为：{1},原始价格：{2}，当前时间：{3}", Charge.Name, Charge.Amount, historyent.Amount, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    FileLog.Info(intostring);
                    return service.Update(Charge);
                }
                else
                {
                    if (service.IQueryable(f => f.Name == Charge.Name && f.State == true).FirstOrDefault() != null)
                    {
                        return -2;
                    }
                    // ChargeName(Charge);
                    Charge.Create();
                    Charge.State = true;
                    string intostring = string.Format("增加了价格对象，标题为：{0}，价格为：{1}，当前时间：{2}", Charge.Name, Charge.Amount, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    FileLog.Info(intostring);
                    return service.Insert(Charge);
                }
            }
            return -1;
        }

    }
}
